package com.sm.healthy.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.sm.healthy.R;


/**
 * Created by sm on 2018/12/28.
 */

public class TableView extends View {

    private final String TAG = "TableView";

    //画安全区域
    private Paint mPaint = new Paint();
    //画表格线条
    private Paint paintAxes2 = new Paint();


    //    private static final int Max_Y = 580;
    //    private static final int Max_X = 1000;
    //    private static final int startX = 10;
    private int startX = 10;//画X轴初始位置PX
    private int startY = 1;//画Y轴初始位置PX
    //    private int endX = 50*15;
    //    private int endY = 10*21;
    private int endX;//原始宽度
    private int endY;//原始高度
    private int LINE_NUM_X = 21;//行数
    private int LINE_NUM_Y = 17;//列数
    private int height;//换算实际高度
    private int width;//换算实际宽度

    private int saveX = 8;//设置安全范围最大值1-20
    private int saveY = 12;//设置安全范围最小值:1-20
    private int moveX = 0;//设置时间移动范围值
    //    private int moveTime = 2;
    private float moveTime = 0.5f;//位移速率
    private String timeStr;//移动时间字符串
    private boolean isMoveEnd = false;//时间是否移动到最大宽度
    //    private boolean isRestPath = true;


    // 2.初始化画笔
    private void initPaint() {

        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.my_color_fff2f2));       //设置画笔颜色
        mPaint.setStyle(Paint.Style.FILL);  //设置画笔模式为填充
        mPaint.setStrokeWidth(2f);         //设置画笔宽度为10px
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setDither(true);//防抖动


        paintAxes2.setStyle(Paint.Style.STROKE);
        paintAxes2.setDither(true);
        paintAxes2.setAntiAlias(true);
        paintAxes2.setColor(ContextCompat.getColor(getContext(), R.color.my_color_666666));
        paintAxes2.setStrokeWidth(1f);         //设置画笔宽度为4px

    }

    public TableView(Context context) {
        super(context);
        initPaint();
    }

    public TableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public TableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthsize = MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式

        int heightsize = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式

        setMeasuredDimension(widthsize, heightsize);
        endX = widthsize;
        endY = heightsize;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        height = (endY - 15) / LINE_NUM_X;
        width = (endX - 15) / LINE_NUM_Y;
        //画安全填充区域
        canvas.drawRect(startX, height * saveX, width * LINE_NUM_Y + startX, height * saveY, mPaint);

        //画表格横线
        for (int x = 0; x <= LINE_NUM_X; x++) {
            canvas.drawLine(startX, startY + (height * x), startX + width * LINE_NUM_Y, startY + (height * x), paintAxes2);
        }
        //画表格竖线
        for (int y = 0; y <= LINE_NUM_Y; y++) {
            canvas.drawLine(startX + (width * y), startY, startX + (width * y), height * LINE_NUM_X, paintAxes2);
        }
        //画文字
        for (int x = 0; x <= LINE_NUM_X; x++) {
            for (int y = 0; y <= LINE_NUM_Y; y++) {
                if (x == 0 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "240");
                } else if (x == 3 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "210");
                } else if (x == 6 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "180");
                } else if (x == 9 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "150");
                } else if (x == 12 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "120");
                } else if (x == 15 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "90");
                } else if (x == 18 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "60");
                } else if (x == 21 && y == 3) {
                    drawCellText(canvas, startX / 2 + (width * y) + width / 2, startY * 2 + (height * (x + 1)) - height / 4, height, "30");
                }
            }
        }
        //绘制时间标记
        if (!TextUtils.isEmpty(timeStr)) {
            //从左往右
            if (isMoveEnd) {
                drawCellText(canvas, (startX * 2 + (moveTime * moveX) - width), startY * 2 + (height * (21 + 1)) - height / 4, height, timeStr);
            } else {
                drawCellText(canvas, (startX + (moveTime * moveX)), startY * 2 + (height * (21 + 1)) - height / 4, height, timeStr);
            }
//            //从右往左
//            if (isMoveEnd) {
//                drawCellText(canvas, width * LINE_NUM_Y - ((moveTime * moveX)), startY * 2 + (height * (21 + 1)) - height / 4, height, timeStr);
//            } else {
//                drawCellText(canvas, width * LINE_NUM_Y - ((moveTime * moveX) - 8.5f), startY * 2 + (height * (21 + 1)) - height / 4, height, timeStr);
//            }
        }
    }

    /**
     * 时间是否移动至末尾
     *
     * @param isMoveEnd
     */
    public void setTimeMoveEnd(boolean isMoveEnd) {
        this.isMoveEnd = isMoveEnd;
    }

    // 绘制单元格中的文字
    private void drawCellText(Canvas canvas, float textX, int textY, int textSize, String text) {
        Paint paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        paint.setTextSize(textSize);
        canvas.drawText(text, textX, textY, paint);
    }

    public void setSaveValue(int saveX, int saveY) {
        this.saveX = saveX;
        this.saveY = saveY;
        invalidate();
    }

    /**
     * 设置时间戳移动
     *
     * @param moveX
     * @param moveTime
     * @param timeStr
     */
    public void setMoveX(int moveX, float moveTime, String timeStr) {
        this.moveX = moveX;
        this.timeStr = timeStr;
        this.moveTime = moveTime;
        invalidate();
    }

    /**
     * 返回View的总宽度
     *
     * @return
     */
    public int getTableWidth() {
        return width * LINE_NUM_Y;
    }

    /**
     * 返回View的总高度
     *
     * @return
     */
    public int getTableHeight() {
        return height * LINE_NUM_X;
    }

    /**
     * 返回View实际可以绘制的高度误差值
     *
     * @return
     */
    public int getTableHeightError() {
        return height * 3;
    }

    /**
     * 返回当前位移宽度（宽度不大于View总宽度）
     *
     * @return
     */
    public float getMoveToatle() {
        return startX + (moveTime * moveX);
    }

    /**
     * 返回当前划线的总长度，长度超出View总长度
     *
     * @param moveLineX 位移次数（不限制）
     * @return
     */
    public float getMoveLineToatle(int moveLineX) {
        return startX + (moveTime * moveLineX);
    }

    //    public void setPointLine(boolean secondDraw) {
    //        this.secondDraw = secondDraw;
    //    }
}
